package tech.aistar.controller;

import org.springframework.web.bind.annotation.PathVariable;
import tech.aistar.entity.Product;
import tech.aistar.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;

@Controller
public class ProductController {
    @Autowired // 依赖于service
    private IProductService productService;


    //获得所有的商品  http://localhost:8080/demo/product
    @GetMapping("product")
    public ModelAndView getAllProduct(){
        ModelAndView mav = new ModelAndView();
        //1. 调用service---mapper ---database 查询
        List<Product> productList = productService.list();
        //2. 数据绑定
        mav.addObject("productList",productList);
        //3. 页面跳转
        mav.setViewName("index");  // index.jsp
        return mav;
    }
    @GetMapping("product/{id}")
    public ModelAndView getById(@PathVariable("id") Integer id){
        Product product = productService.getById(id);
        ModelAndView mav = new ModelAndView("detail");  // /detail.jsp
        mav.addObject("product",product);
        return mav;
    }
}
